package com.handinfo.android.core.media;

import java.io.IOException;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.net.Uri;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.resource.DWSerializableFactory;
import com.handinfo.android.core.resource.IORWFile;
import com.handinfo.android.utils.Tools;

/**
 * 背景音乐 MediaPlayer
 * 
 * 音效 SoundPool
 * 
 * @author
 * 
 */
public class DWAudio
{
	private Context m_context;
	private HashMap<Byte, Integer> m_soundMap;
	private AudioManager m_audioManager;
	private static DWAudio sIntance;

	public static final byte m_副本1 = 0;
	public static final byte m_新手村 = 1;
	public static final byte m_野外 = 2;
	public static final byte m_主界面音乐 = 3;
	public static final byte m_人族主城 = 4;
	public static final byte m_副本2有压迫感紧张 = 5;
	public static final byte m_战场1铁血武侠精心动魄 = 6;
	public static final byte m_战场2家族战场 = 7;

	public static final byte mSOUND_NUM = 25; //音效的数量
	public static final byte m_开启菜单选项 = 0;
	public static final byte m_关闭菜单选项 = 1;
	public static final byte m_选定确认操作 = 2;
	public static final byte m_新邮件通知 = 3;
	public static final byte m_装备被分解 = 4;
	public static final byte m_升级 = 5;
	public static final byte m_轻兵器的斩击声1 = 6;
	public static final byte m_轻兵器的斩击声2 = 7;
	public static final byte m_重兵器的斩击声1 = 8;
	public static final byte m_拂尘抽动的声音 = 9;
	public static final byte m_法术施放的特效声1 = 10;
	public static final byte m_火球砸中发出的撞击和燃烧声 = 11;
	public static final byte m_爆炸声1 = 12;
	public static final byte m_冰块破碎的声音 = 13;
	public static final byte m_液体沸腾的水泡声 = 14;
	public static final byte m_龙卷风极速掠过地面发出的声音 = 15;
	public static final byte m_火焰喷射而出发出的呼呼声1 = 16;
	public static final byte m_火焰喷射而出发出的呼呼声2 = 17;
	public static final byte m_刃物相打而发出的碰撞声 = 18;
	public static final byte m_离弦之箭破过空气发出的声响 = 19;
	public static final byte m_火焰陡然猛烈燃烧发出的声响 = 20;
	public static final byte m_瀑布 = 21;
	public static final byte m_强化成功 = 22;
	public static final byte m_升级2 = 23;
	public static final byte m_树藤 = 24;
	
	/* 音效开关 */
	private boolean m_se_pause;
	
	public boolean isSEPause()
	{
		return m_se_pause;
	}
	
	public void setSEPause(boolean pause)
	{
		m_se_pause = pause;
	}

	private DWAudio(Context context)
	{
		m_context = context;
		((Activity) m_context).setVolumeControlStream(AudioManager.STREAM_MUSIC);
		m_audioManager = (AudioManager) m_context.getSystemService(Context.AUDIO_SERVICE);
		init();
	}

	public static DWAudio getInstance()
	{
		if (sIntance == null) {
			sIntance = new DWAudio(DWGameManager.getInstance().getCurrentActivity());
		}
		return sIntance;
	}

	private static MediaPlayer m_current_music; //当前播放的音乐
	private static int m_current_musicId;
	private SoundPool m_soundPool;

	public void init()
	{
		m_soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
		m_soundMap = new HashMap<Byte, Integer>();
	}
	
	public void setMusicId(int id) 
	{
		m_current_musicId = id;
	}

	public void loadMusic(int id)
	{
		try {
			releaseMusic();
			m_current_musicId = id;
			String pathName = DWGameManager.s_res_dir + musicName + id + DWSerializableFactory.EXTENSION_SOUND;
			if (pathName != null) {
				m_current_music = new MediaPlayer();
				m_current_music.setDataSource(pathName);
//				m_current_music = MediaPlayer.create(DWGameManager.getInstance().getCurrentActivity(), Uri
//						.parse(pathName));
//				m_current_music.prepare();
				m_current_music.setLooping(true);
//				m_current_music.reset();

			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}

	}

	String musicName = "/sound/music_";

	public void playMusic()
	{
		if ((DWGameManager.s_setting_system & DWGameManager.设置系统_游戏音乐) == 0)
			return;
		try {
			if (m_current_music != null) {
				m_current_music.prepare();
				m_current_music.start();
			}
			else {
//				loadMusic(m_current_musicId);
//				if (m_current_music != null) {
//					m_current_music.start();
//				}
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public void pauseMusic()
	{
		try {
			if (m_current_music != null) {
				if (m_current_music.isPlaying()) {
					m_current_music.pause();
				}
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}
	
	public void resumeMusic()
	{
		if ((DWGameManager.s_setting_system & DWGameManager.设置系统_游戏音乐) == 0)
			return;
		try {
			if (m_current_music != null) {
				if (!m_current_music.isPlaying()) {
					m_current_music.start();
				}
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public void stopMusic()
	{
		try {
			if (m_current_music != null) {
				if (m_current_music.isPlaying()) {
					m_current_music.stop();
				}
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public void releaseMusic()
	{
		try {
			if (m_current_music != null) {
				if (m_current_music.isPlaying()) {
					m_current_music.stop();
				}
				m_current_music.release();
				m_current_music = null;
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	String soundName = "/sound/sound_";

	public void loadSoundPool(byte resid)
	{
		try {
			String pathName = DWGameManager.s_res_dir + soundName + resid + DWSerializableFactory.EXTENSION_SOUND;
			int soId = m_soundPool.load(pathName, 1);
			m_soundMap.put(resid, soId);
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public void loadSoundPool(byte[] resIds)
	{
		try {
			if (resIds != null) {
				for (byte id : resIds) {
					String pathName = DWGameManager.s_res_dir + soundName + id + DWSerializableFactory.EXTENSION_SOUND;
					int soId = m_soundPool.load(pathName, 1);
					m_soundMap.put(id, soId);
				}
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	public void playSound(int resid)
	{
		try {
			if ((DWGameManager.s_setting_system & DWGameManager.设置系统_游戏音效) == 0)
				return;
			if (m_se_pause)
				return;
			Integer id = m_soundMap.get((byte) resid);
			if (id == null)
				return;
			m_soundPool.play(id, 1, 1, 1, 0, 1);		
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}


	/**
	 * 音量
	 * @param level
	 */
	public void setVolume(int level)
	{
		level = Tools.limit(level, 0, m_audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
		m_audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, level, AudioManager.FLAG_PLAY_SOUND);
	}

	public void adjustVolume(boolean raise)
	{
		if (raise) {
			m_audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,
					AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
		}
		else {
			m_audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,
					AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
		}
	}
}
